package com.ww.springboot.boot.javabase.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

public class EnhancerMethodInterceptor {
 
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Car.class);
        enhancer.setCallback((MethodInterceptor) (obj, method, args1, methodProxy) -> {
            System.out.println(method);
            if(!method.getName().equals("print")){
                return null;
            }
            System.out.println("before");
            Object res = methodProxy.invokeSuper(obj, args1);
            System.out.println("after");
            return res;
        });
        Car car = (Car) enhancer.create();
 
        car.print();
    }
 
    static class Car {
        void print() {
            System.out.println("I am a car");
        }
    }
 
}